// /rule/addRuleInfo.do
document.write('<script type="text/javascript" src="../static/js/back/utils.js"></script>');
document.write('<script type="text/javascript" src="../static/js/back/ajax.js"></script>');
var _url_ = '../goods/addGoodsInfo.do';
var _url_edit = '../act/updateActivityInfo.do';
var _url_list = '../act/getActivityList';
var _url_id = '../goods/getGoodsById';
var _map = {};
///首页
var _pageNum = 1;
var _type ;
var spid="";
  $(function(){
	   
	   spid = getCookie("shop_id");
	  _type = getUrlParam("type");
	  console.log(_type)
	  if(_type === "add"){
		  getTypeListAdd();
		  $("#shopId").val(spid);
	  }else if(_type === "edit"){
		  _url_ = _url_edit;
//		  getTypeListAdd();
//		  $(".is_up_do").hide();
//		  showEditData();
	  }else{
		  //获取分类列表
//		  getTypeList();
		  getTypeCount();
		  getList();
	  }
  })
$(function(){
	  $("body").on("click",".mypage span",function(){
		  _pageNum = $(this).attr("data-status");
		  getList();
	  })
	  $(".search-btn").click(function(){
			 var tit =  $("#goodsTitle").val();
			 var stts =  $("#status").val();
			 console.log(tit);
			 _map["goodsTitle"] = tit;
			 _map["status"] = stts;
			 _pageNum = 1;
			 getList();
		  });
	  $("body").on("click",".left-type li",function(){
			 var tt = $(this).text();
			 $(this).addClass('lt-li-active').siblings().removeClass('lt-li-active');
			 _map['goodsTypeId'] = $(this).attr('data-status');
			 getList();
	  });
});
  function getList(){
	  console.log('sdfsfasdfasdfasdfasdf');
	  _map['shopId'] = spid;
	  _ajax_get(_url_list,_map,function(res){
		  console.log(res.returnObject);
//		  console.log(res.returnObject.length);
		  $.each(res.returnObject,function(i,v,arr){
			  console.log(v);
			  if(v.activityType == 1 ){
				  $("#act-1").find('td').eq(1).find('span').text((Number(v.content) / 100).toFixed(2));
			  }else if(v.activityType == 2 || v.activityType == 4 ){
				  var conss = v.content.split(',');
				  console.log(conss);
				  var conStr = "";
				  for (var k = 0; k < conss.length; k++) {
					 var cons = conss[k];
					 
					 conStr += '满 '+(cons.split('-')[0] / 100).toFixed(2)+' 减'+(cons.split('-')[1] / 100).toFixed(2)  + ' 元';
					 if((k+1) != conss.length){
						 conStr += '; ';
					 }
				  }
				  $("#act-"+v.activityType).find('td').eq(1).html(conStr);
			  }else if(v.activityType == 3){
				  
			  }else if(v.activityType == 5){
				  $("#act-5").find('td').eq(1).find('span').text((Number(v.content) / 10).toFixed(1));
			  }
			  $("#act-"+v.activityType).find('td').eq(3).text(v.status==1?'已开启':'已关闭');
			  $("#ud-"+v.activityType).attr('data-status',v.status==1?'2':'1').attr("actt-id",v.activityType);
			  
			  $("#ud-"+v.activityType).find('i').text(v.status==1?'关闭':'开启').attr('title',v.status==1?'关闭':'开启');
		  })
		  //$(".layui-table tbody").html(html);
	  })
  }
  
  function getTypeList(){
	  
	  _ajax_post_token('../goods/getGoodsTypeList',{pageSize:100,shopId:spid},function(res){
		  var list =  res.returnObject;
		  var html = '<li data-status="" class="lt-li-active">全部</li>';;
		  $.each(list.list,function(i,v,arr){
    		  if(list){
    			 html+='<li data-status="'+v.id+'">'+v.title+'</li>';
    		  }
		  })
		  $(".left-type ul").html(html);
	  })
  }
  function getTypeCount(){
	  _ajax_post_token('../act/getActivityDisList',{pageSize:100,shopId:spid},function(res){
		  console.log(res);
		  $("#act-3").find('td').eq(1).find('span').text(res.returnObject.total);
	  })
  }
  
  
  function getTypeListAdd(){
	  console.log(spid);
	  _ajax_post_token('../goods/getGoodsTypeList',{pageSize:100},function(res){
		  console.log(res);
		  var list =  res.returnObject;
		  var html = "";
		  $.each(list.list,function(i,v,arr){
    		  if(list){
    			 html+='<option value="'+v.id+'">'+v.title+'</option>';
    		  }
		  })
		  $(".pIdsel").append(html);
	  })
  }
  function showEditData(){
	  _url_ = _url_edit;
	  var _id = getUrlParam("id");
	  $("#paramKey").val(_id);
	  console.log(_id);
	  _ajax_post_token(_url_id,{userType:1,id:_id},function(res){
		  console.log(res);
		 var ddt = res.returnObject;
		 ddt.priceOut = (ddt.priceOut/100).toFixed(2);
		 ddt.priceRoom = (ddt.priceRoom/100).toFixed(2);
		 ddt.goodsPackAmount = (ddt.goodsPackAmount/100).toFixed(2);
		  _showData = ddt;
	  });
  }    
      
  /*用户-删除*/
  function member_del(obj,_id){
      layer.confirm('确认要删除吗？',function(index){
          //发异步删除数据
    	  _url_ =_url_edit;
    	  _ajax_post_token(_url_,{status:0,id:_id},function(res){
    	      	console.log(res);
    	      	if(res.resultCode == 1000){
    	      		getList();
    	      	  layer.msg('删除成功!',{icon:6,time:2000});
    	      	}else{
    	      		layer.msg('删除失败!',{icon:5,time:2000});
    	      	}
    	      });
      });
  }
  /*上下架*/
  function member_ud(obj,_id){
	  	  
	  	  var stats = $(obj).attr('data-status');
	  	  var typ = $(obj).attr('actt-id');
	  	  
		  //发异步删除数据
		  _url_ =_url_edit;
		  _ajax_post_token(_url_,{status:stats,shopId:spid,activityType:typ},function(res){
			  console.log(res);
			  if(res.resultCode == 1000){
				  getList();
				  layer.msg('操作成功!',{icon: 6,time:1000});
			  }else if(res.resultCode == 1009){
				  layer.msg('操作失败，请先编辑活动内容',{icon:5,time:2000});
			  }else{
				  layer.msg('操作失败!',{icon:5,time:2000});
			  }
		  });
  }
  //批量上架
  function member_udall(){
	  var data = tableCheck.getData().join(',');
	  _ajax_post_token(_url_edit,{status:1,ids:data},function(res){
		  console.log(res);
		  if(res.resultCode == 1000){
			  getList();
			  layer.msg('操作成功!',{icon: 6,time:1000});
		  }else{
			  layer.msg('操作失败!',{icon:5,time:2000});
		  }
	  });
  }
  function member_udall2(){
	  var data = tableCheck.getData().join(',');
	  _ajax_post_token(_url_edit,{status:2,ids:data},function(res){
		  console.log(res);
		  if(res.resultCode == 1000){
			  getList();
			  layer.msg('操作成功!',{icon: 1,time:1000});
		  }else{
			  layer.msg('操作失败!',{icon:5,time:2000});
		  }
	  });
  }
  
  
  /*用户-启动*/
  function member_start(obj,_id){
	  layer.confirm('确认要解封此账号吗？',function(index){
		  //发异步删除数据
		  _url_ =_url_edit;
		  _ajax_post_token(_url_,{userStatus:1,id:_id},function(res){
			  console.log(res);
			  if(res.resultCode == 1000){
				  getList();
				  layer.msg('已解封!',{icon: 6,time:1000});
			  }else if (res.resultCode == 1008){
				  layer.msg('当前账号已存在',{icon:5,time:2000});
			  }
			  else{
				  layer.msg('操作失败!',{icon:5,time:2000});
			  }
		  });
	  });
  }
      
  function updateInfo(data){
	  //发异步，把数据提交给后台
	 console.log(_url_);
	 data['shopId'] = spid;
      _ajax_post_token(_url_,data,function(res){
      	console.log(res);
      	if(res.resultCode == 1000){
      	  parent.getList();
      	  parent.layer.msg('操作成功!',{icon:1,time:2000});
      	  x_admin_close();
          // 获得frame索引
      	}else{
      		layer.msg('操作失败!',{icon:1,time:2000});
      	}
      });
  }
      
 
      
//添加 和 修改窗口     